from typing import List

class Solution:
    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        m = len(board)
        n = len(board[0])

        def in_area(x, y):
            return x >= 0 and y >= 0 and x < m and y < n

        visited = []
        dir = ((-1, -1), (-1, 0), (-1, +1), (0, -1), (0, +1), (1, -1), (1, 0), (1, 1))
        # 结束
        if board[click[0]][click[1]] == "M":
            board[click[0]][click[1]] == "X"
            return board

        def helper(x, y):
            cnt = 0
            for x_, y_ in dir:
                tx, ty = x+x_,y+y_
                if not in_area(tx, ty):
                    continue

                if board[tx][ty] == "M":
                    cnt += 1
            
            if cnt > 0:
                board[x][y] = str(cnt)
            else:
                board[x][y] = "B"
                for x_, y_ in dir:
                    tx, ty = x+x_, y+y_
                    helper(tx, ty)
        helper(click[0], click[1])

board= [['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'M', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E']]
click = [3, 0]

Solution().updateBoard(board, click)